﻿Imports DAO
Imports BUS
Public Class ucTimKiem

    Private Sub rdoTenSach_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdoTenSach.CheckedChanged
        txtTenSach.Enabled = rdoTenSach.Checked
    End Sub

    Private Sub rdoTheLoai_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdoTheLoai.CheckedChanged
        cboTheLoai.Enabled = rdoTheLoai.Checked
    End Sub

    Private Sub rdoTenTG_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdoTenTG.CheckedChanged
        txtTenTG.Enabled = rdoTenTG.Checked
    End Sub

    Private Sub btnTimSach_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTimSach.Click
        Try
            Dim sBUS As New SachBUS

            If rdoTenSach.Checked Then
                Dim saches = sBUS.TimKiemTheoTenSach(txtTenSach.Text)
                dgvTraCuuSach.DataSource = saches
            Else
                If rdoTenTG.Checked Then
                    Dim saches = sBUS.TimKiemTheoTenTG(txtTenTG.Text)
                    dgvTraCuuSach.DataSource = saches
                Else
                    Dim maTL As Integer
                    Integer.TryParse(cboTheLoai.SelectedValue.ToString(), maTL)
                    Dim saches = sBUS.TimKiemTheoTheLoai(maTL)
                    dgvTraCuuSach.DataSource = saches
                End If
            End If
        Catch ex As Exception
            dgvTraCuuSach.Rows.Clear()
        End Try
        
        dgvTraCuuSach.Columns("TenSach").HeaderText = "Tên Sách"
        dgvTraCuuSach.Columns("TenTL").HeaderText = "Thể Loại"
        dgvTraCuuSach.Columns("TenTG").HeaderText = "Tên TG"
        dgvTraCuuSach.Columns("SoLuong").HeaderText = "Số Lượng"
        dgvTraCuuSach.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
        
    End Sub

    Private Sub ucTimKiem_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim tlBUS As New TheLoaiBUS
        cboTheLoai.DataSource = tlBUS.LayBang()
        cboTheLoai.DisplayMember = "TenTL"
        cboTheLoai.ValueMember = "MaTL"
        cboTheLoai.SelectedIndex = 0
    End Sub
End Class
